/**
 * grayrabbit Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package com.grayrabbit.slacker.gengerate;

import java.util.Map;

import com.grayrabbit.slacker.config.GenerateConfig;
import com.grayrabbit.slacker.constant.PropertyConsant;
import com.grayrabbit.slacker.interfaces.CodeGenerate;
import com.grayrabbit.slacker.utils.TemplateProcessUtils;

/**
 * 视图页面添加修改页面生成工具类
 * 
 * @author liyd
 * @version $Id: ViewPageInputGenerate.java, v 0.1 2012-5-17 下午4:49:36 liyd Exp $
 */
public class ViewPageInputGenerate extends NormalGenerate implements CodeGenerate {

    /** 
     * @see com.grayrabbit.slacker.interfaces.CodeGenerate#generate(com.grayrabbit.slacker.config.GenerateConfig, java.util.Map)
     */
    public void generate(GenerateConfig generateConfig, Map<String, String> modelMap) {

        super.normalDataInit(generateConfig);

        //不包含包名
        String actionClassName = this.getClassName(ACTION_QUALIFIED_CLASS_NAME);

        //模板文件夹
        String templateDir = TemplateProcessUtils.getTargetDirPath(generateConfig);

        String templateName = getActionName(actionClassName)
                              + TemplateProcessUtils.getEndFix(generateConfig)
                              + TemplateProcessUtils.getSuffix(generateConfig);

        //需要指定生成的文件名，根据action拆分
        generateConfig
            .setTargetFilePath(templateDir + PropertyConsant.DIR_SEPARATOR + templateName);

        //列字段
        StringBuilder textField = new StringBuilder();
        //js校验属性
        StringBuilder validationField = new StringBuilder();

        String primaryKey = null;
        int count = 0;
        //根据属性生成
        for (String[] column : generateConfig.getColumns()) {

            //属性名称
            String propertyName = TemplateProcessUtils.getPropertyName(column[1]);

            //属性备注
            String propertyComment = column[2];

            //创建字段
            textField.append(getTextField(generateConfig,
                this.getClassName(ENTITY_QUALIFIED_CLASS_NAME), propertyName, propertyComment));

            if (count == 0) {
                primaryKey = propertyName;
            }

            count++;
            //格式为： 'district.parentId' : 'required',
            validationField.append("\t\t\t\t'"
                                   + TemplateProcessUtils.convertFirstLower(this
                                       .getClassName(ENTITY_QUALIFIED_CLASS_NAME)) + "."
                                   + propertyName + "' : 'required'");
            if (count != generateConfig.getColumns().size()) {
                validationField.append(",");
                validationField.append("\r\n");
            }
        }

        modelMap.put("entityInstance",
            TemplateProcessUtils.convertFirstLower(this.getClassName(ENTITY_QUALIFIED_CLASS_NAME)));

        modelMap.put("actionName", getActionName(actionClassName));

        modelMap.put("primaryKey", primaryKey);

        modelMap.put("textField", textField.toString());

        modelMap.put("validationField", validationField.toString());

        String srcDir = TemplateProcessUtils.getSrcDir(generateConfig);

        modelMap.put("namespace", "");

        if (srcDir.indexOf("templates/") != -1) {
            String namespace = srcDir.substring(srcDir.lastIndexOf("templates") + 9);
            while (namespace.indexOf("//") != -1) {
                namespace = namespace.replace("//", "/");
            }
            if (!"/".equals(namespace)) {
                namespace = namespace.startsWith("/") ? namespace.substring(1) : namespace;
                namespace = namespace.endsWith("/") ? namespace : namespace + "/";
                modelMap.put("namespace", namespace);
            }
        }

    }

    /**
     * 单个字段vm
     * 
     * @param generateConfig
     * @param entityClassName
     * @param propertyName
     * @param propertyComment
     * @return
     */
    private String getTextField(GenerateConfig generateConfig, String entityClassName,
                                String propertyName, String propertyComment) {

        //属性模板文件
        String vm = TemplateProcessUtils.getTemplate(generateConfig.getProperties()
            .get(PropertyConsant.TEMPLATE).getChildProperties().get("fieldTemplate").getValue());

        vm = vm.replaceAll("\\$\\{propertyComment\\}", propertyComment);
        vm = vm.replaceAll("\\$\\{propertyName\\}", propertyName);
        vm = vm.replaceAll("\\$\\{entityInstance\\}",
            TemplateProcessUtils.convertFirstLower(entityClassName));

        return vm;

    }

}
